<?php

namespace app\admin\controller;

use app\admin\common\Base;
use think\Db;
use think\Session;

use app\admin\model\AuthUser as AuthUserModel;
use app\system\model\AuthUser;

class Staff extends Base
{
    public function index()
    {
        $data = $this->request->param();

        if (isset($data['ajax']) && $data['ajax'] == 100) {
            $map = '';
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);

            //搜索功能
            if(isset($data['matchCon']) && !empty($data['matchCon'])){
 
                $list = Db('auth_user')
                       ->where(['is_del' => 0,'enterprise_id' => $this->EnterpriseId])
                       ->where('sn','like' , '%' . $data['matchCon'] . '%')
                       ->whereOr('name','like' , '%' . $data['matchCon'] . '%')
                       ->whereOr('telephone','like' , '%' . $data['matchCon'] . '%')
                       ->order('id asc')
                       ->limit($start, $limit)
                       ->select();
                $count = count($list);

            }else{
               
                if ($map == '') {
                    $list = Db::name('auth_user')->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->order('id asc')->limit($start, $limit)->select();
                    $count = Db::name('auth_user')->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
                } else {
                    $list = Db::name('auth_user')->where('is_del', 0)->order('id asc')->where('enterprise_id', $this->EnterpriseId)->limit($start, $limit)->select();
                    $count = Db::name('auth_user')->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
                }

            }
            


            return $this->grid_data($list, $count, $page, $limit);
        }
        return $this->fetch();
    }

    public function add()
    {
        return $this->fetch();
    }

    //编辑栏目1-1
    public function edit($id)
    {
        $info = Db::name('auth_user')->where('id', $id)->find();
        $this->assign('info', $info);
        return $this->fetch();
    }
    //编辑栏目1-2
    public function save()
    {
        $status = 0;
        $msg = "操作失败";
        $data = $this->request->param();
        $model = new AuthUserModel();
        if (empty($data['id'])) {
            $data['enterprise_id'] = $this->EnterpriseId;
            $result = $model->allowField(true)->save($data);
            if ($result) {
                $status = 1;
                $msg = "添加成功";
            }
        } else {
            $id = $data['id'];
            $result = $model->allowField(true)->save($data, ["id" => $id]);
            if ($result) {
                $status = 1;
                $msg = "更新成功";
            }
        }
        return ['status' => $status, 'msg' => $msg];
    }

    //删除栏目1-1
    public function del($id)
    {
        $status = 0;
        $msg = "操作失败";
        $del = Db::name('auth_user')->where('id', $id)->update(["is_del" => 1]);
        if ($del) {
            $status = 1;
            $msg = "删除成功";
        }
        return ['status' => $status, 'msg' => $msg];
    }
}
